/*
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */
package org.nuxeo.gwt.habyt.templates.client.bindings;

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.ResetButton;
import com.google.gwt.user.client.ui.SimpleCheckBox;
import com.google.gwt.user.client.ui.SimpleRadioButton;
import com.google.gwt.user.client.ui.SubmitButton;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
 * 
 */
public class BuiltinWidgets {

    public static class BTextBox extends TextBox {
        public BTextBox(Element element) {
            super(element);
        }
    }

    public static class BPasswordTextBox extends PasswordTextBox {
        public BPasswordTextBox(Element element) {
            super(element);
        }
    }

    public static class BTextArea extends TextArea {
        public BTextArea(Element element) {
            super(element);
        }
    }

    public static class BButton extends Button {
        public BButton(Element element) {
            super(element);
        }

    }

    public static class BCheckBox extends SimpleCheckBox {
        public BCheckBox(Element element) {
            super(element);
        }
    }

    public static class BRadioButton extends SimpleRadioButton {
        public BRadioButton(Element element) {
            super(element);
        }
    }

    public static class BHidden extends Hidden {
        public BHidden(Element element) {
            super(element);
        }
    }

    public static class BFileUpload extends FileUpload {
        public BFileUpload(Element element) {
            super(element);
        }
    }

    public static class BListBox extends ListBox {
        public BListBox(Element element) {
            super(element);
        }
    }

    public static class BSubmitButton extends SubmitButton {
        public BSubmitButton(Element element) {
            super(element);
        }
    }

    public static class BResetButton extends ResetButton {
        public BResetButton(Element element) {
            super(element);
        }
    }

    public static class BHTML extends HTML {
        public BHTML(Element element) {
            super(element);
        }
    }

    public static class BInlineHTML extends InlineHTML {
        public BInlineHTML(Element element) {
            super(element);
        }
    }

    public static class BAnchor extends Anchor {
        public BAnchor(Element element) {
            super(element);
        }
    }

    public static class BImage extends Image {
        public BImage(Element element) {
            super(element);
        }
    }

    public static Widget getWidget(Element node) {
        com.google.gwt.user.client.Element element = node.<com.google.gwt.user.client.Element> cast();
        String tag = node.getTagName().toLowerCase();
        if ("button".equals(tag)) {
            return new BButton(element);
        } else if ("a".equals(tag)) {
            return new BAnchor(element);
        } else if ("img".equals(tag)) {
            return new BImage(element);
        } else if ("select".equals(tag)) {
            return new BListBox(element);
        } else if ("textarea".equals(tag)) {
            return new BTextArea(element);
        } else if ("div".equals(tag)) {
            return new BHTML(element);
        } else if ("span".equals(tag)) {
            return new BInlineHTML(element);
        } else if ("input".equals(tag)) {
            String type = Helper.getAttribute(node, "type");
            if ("text".equals(type)) {
                return new BTextBox(element);
            } else if ("checkbox".equals(type)) {
                return new BCheckBox(element);
            } else if ("password".equals(type)) {
                return new BPasswordTextBox(element);
            } else if ("radio".equals(type)) {
                return new BRadioButton(element);
            } else if ("submit".equals(type)) {
                return new BSubmitButton(element);
            } else if ("file".equals(type)) {
                return new BFileUpload(element);
            } else if ("hidden".equals(type)) {
                return new BHidden(element);
            } else if ("image".equals(type)) {
                return new BImage(element);
            } else if ("button".equals(type)) {
                return new BButton(element);
            } else if ("reset".equals(type)) {
                return new BResetButton(element);
            }
        } else if ("form".equals(tag)) {
            // return new BForm(element);
        }
        System.out.println(element);
        return null;
    }
}
